var sqlpad = {};
(function (po) {
    //SQL编辑器，调用po.initSqlEditor()后初始化
    // po.sqlEditor = undefined;

    po.getSqlEditorElementId = function () {
        return "editor";
    };

    po.getSqlEditorAutocompleteAjaxOptions = function (autocompleteInfo) {
        var url = "/v1.0/sqlpad/presto/";

        if (autocompleteInfo.type == "catalog") {
            url += "catalogs";
            return {"url": url};
        }

        if (autocompleteInfo.type == "schema") {
            url += "schemas";
            var data = {"catalog": ""};
            data.catalog = autocompleteInfo.catalog;
            return {"url": url, "data": data};
        }

        if (autocompleteInfo.type == "table") {
            url += "tables";
            var data = {"catalog": "", "schema": ""};
            data.catalog = autocompleteInfo.catalog;
            data.schema = autocompleteInfo.schema;
            return {"url": url, "data": data};
        }

        if (autocompleteInfo.type == "column") {
            url += "columns";
            var data = {"catalog": "", "schema": "", "table": ""};
            data.catalog = autocompleteInfo.catalog;
            data.schema = autocompleteInfo.schema;
            data.table = autocompleteInfo.table;
            return {"url": url, "data": data};
        }

    };


    po.getSqlAutocompleteCompletions = function (editor, session, pos, prefix, callback) {

        var info = $.sqlAutocomplete.resolveAutocompleteInfo(editor, session, pos, prefix, ";");

        var catalogAlias = (info.catalog ? info.catalog + "." : "");
        var schemaAlias = (info.schema ? info.catalog + "." + info.schema + "." : "");
        var tableAlias = (info.table ? info.catalog + "." + info.schema + "." + info.table + "." : "");

        if (info && info.type == "catalog" && po.sqlAutocompleteCatalogCompletions) {
            callback(null, po.sqlAutocompleteCatalogCompletions);
            return;
        }

        if (info && info.type == "schema" && po.sqlAutocompleteSchemaCompletions && po.sqlAutocompleteSchemaCompletions.get(catalogAlias)) {
            callback(null, po.sqlAutocompleteSchemaCompletions.get(catalogAlias));
            return;
        }

        if (info && info.type == "table" && po.sqlAutocompleteTableCompletions && po.sqlAutocompleteTableCompletions.get(schemaAlias)) {
            callback(null, po.sqlAutocompleteTableCompletions.get(schemaAlias));
            return;
        }


        if (info && info.type == "column" && info.table && po.sqlAutocompleteColumnCompletions && po.sqlAutocompleteColumnCompletions.get(tableAlias)) {

            callback(null, po.sqlAutocompleteColumnCompletions.get(tableAlias));
            return;
        }

        if (info && (info.type == "catalog" || info.type == "schema" || info.type == "table" || (info.type == "column" && info.table))) {
            var ajaxOptions =
                {
                    type: "GET",
                    success: function (names) {
                        var completions;

                        if (info.type == "catalog") {
                            completions = $.sqlAutocomplete.buildCompletions(names,null, info.type);
                            po.sqlAutocompleteCatalogCompletions = completions;
                        }

                        if (info.type == "schema") {

                            completions = $.sqlAutocomplete.buildCompletions(names, catalogAlias, info.type);
                            po.sqlAutocompleteSchemaCompletions = new Map();
                            po.sqlAutocompleteSchemaCompletions.set(catalogAlias, completions);
                        }

                        if (info.type == "table") {

                            completions = $.sqlAutocomplete.buildCompletions(names, schemaAlias, info.type);
                            po.sqlAutocompleteTableCompletions = new Map();
                            po.sqlAutocompleteTableCompletions.set(schemaAlias, completions);
                        } else if (info.type == "column") {

                            completions = $.sqlAutocomplete.buildCompletions(names, null, info.type);

                            if (!po.sqlAutocompleteColumnCompletions)
                                po.sqlAutocompleteColumnCompletions = new Map();

                            if (names && names.length > 0) {
                                po.sqlAutocompleteColumnCompletions.set(tableAlias, completions);
                            }
                        }

                        callback(null, completions);
                    },
                    error: function () {
                    }
                };

            $.extend(ajaxOptions, po.getSqlEditorAutocompleteAjaxOptions(info));

            $.ajax(ajaxOptions);
        } else
            callback(null, []);
    };

    po.sqlEditorCompleters =
        [
            {
                identifierRegexps : [/[a-zA-Z_0-9\.\$]/],
                getCompletions: function(editor, session, pos, prefix, callback)
                {
                    po.getSqlAutocompleteCompletions(editor, session, pos, prefix, callback);
                }
            }
        ];

    po.initSqlEditor = function () {
        var languageTools = ace.require("ace/ext/language_tools");

        languageTools.addCompleter({
            identifierRegexps: [/[a-zA-Z_0-9\.\$]/],
            getCompletions: function (editor, session, pos, prefix, callback) {
                po.getSqlAutocompleteCompletions(editor, session, pos, prefix, callback);
            }
        });
        var SqlMode = ace.require("ace/mode/sql").Mode;
        // var SqlMode = ace.require("ace/mode/sql");
        po.sqlEditor = ace.edit(po.getSqlEditorElementId());
        po.sqlEditor.session.setMode(new SqlMode());
        po.sqlEditor.setShowPrintMargin(false);
        po.sqlEditor.setOptions(
            {
                enableBasicAutocompletion: true,
                enableLiveAutocompletion: true
            });

        po.sqlEditor.setOption("wrap", "free");

        return po.sqlEditor;
    };

    po.initCdnSqlEditor = function () {
        var languageTools = ace.require("ace/ext/language_tools");

        languageTools.addCompleter({
            identifierRegexps: [/[a-zA-Z_0-9\.\$]/],
            getCompletions: function (editor, session, pos, prefix, callback) {
                po.getSqlAutocompleteCompletions(editor, session, pos, prefix, callback);
            }
        });

        var editor = ace.edit("editor");
        editor.setTheme("ace/theme/monokai"); //主题
        editor.getSession().setMode("ace/mode/sql"); // 语言高亮
        //自动换行,设置为off关闭
        editor.setOption("wrap", "free");
        //以下部分是设置输入代码提示的
        editor.setOptions({
            enableBasicAutocompletion: true,
            enableSnippets: true,
            enableLiveAutocompletion: true
        });
        editor.getSession().setUseWrapMode(true); //支持代码折叠
        editor.getSession().setUseSoftTabs(true);
        return po.sqlEditor;
    };

    po.initCdnSqlEditor();

})(sqlpad);